package com.ys.chatserver.rtc;

import com.alibaba.fastjson.JSONObject;
import com.eva.epc.common.util.CommonUtils;
import com.eva.framework.utils.LoggerFactory;
import com.ys.chatserver.common.dto.cnst.RtcRedisKeyConstants;
import com.ys.chatserver.im.dto.CMDSosRequest;
import com.ys.chatserver.im.impl.ChatLogicManager;
import com.ys.chatserver.redis.RedisUtil;
import org.slf4j.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RtcStreamNotFoundServlet extends HttpServlet {

    Logger logger = LoggerFactory.getLog();

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String str = req.getReader().readLine();
        logger.info("-------------------------------------------------------------------not found ：{}---------------------------------------------------------", str);
        RtcStreamNotFound obj = JSONObject.parseObject(str, RtcStreamNotFound.class);
        try {
            dispatch(obj, resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected void dispatch(RtcStreamNotFound prRequest, HttpServletResponse resp) throws Exception {
        if (prRequest.getApp().equals("ptt") && prRequest.getParams().contains("play")) {
            //频道对讲 头+群ID+频道ID+用户ID+时间戳
            String[] strings = prRequest.getStream().split("-");
            if (!prRequest.getStream().contains("SOS") && !prRequest.getStream().contains("ONEKEY") && !prRequest.getStream().contains("PHONE")) {
                String groupId = strings[1];
                String[] split = groupId.split("_");
                String userId = strings[3];
                String key = RtcRedisKeyConstants.GET_PTT_GROUP_KEY(split[0], split[1]);
                if (RedisUtil.exists(key)) {
                    RedisUtil.del(key);
                }
                CMDSosRequest cmdSosRequest = new CMDSosRequest();
                cmdSosRequest.setT(groupId);
                cmdSosRequest.setF(userId);
                cmdSosRequest.setM(split[1]);
                ChatLogicManager.processMT56_OF_SOS_NOTIFICATION_SUCCESS_REMOVE_KEY(cmdSosRequest, prRequest.getStream(), false);
                logger.info("pttpttpttpttpttpttpttpttpttptt");
            } else if (prRequest.getStream().contains("SOS")) {
                //全频对讲SOS  头+SOS+群ID+频道ID+用户ID+时间戳
                String groupId = strings[2];
                String[] split = groupId.split("_");
                String userId = strings[4];
                String key = RtcRedisKeyConstants.GET_SOS_GROUP_KEY(split[0], split[1]);
                if (RedisUtil.exists(key)) {
                    RedisUtil.del(key);
                }
                CMDSosRequest cmdSosRequest = new CMDSosRequest();
                cmdSosRequest.setT(groupId);
                cmdSosRequest.setF(userId);
                ChatLogicManager.processMT63_OF_SOS_ALL_NOTIFICATION_SUCCESS_REMOVE_KEY(cmdSosRequest, prRequest.getStream());
                logger.info("sossosososososososososososososo");
            }
        }
        if (prRequest.getApp().equals("live")) {
            String stream = prRequest.getParams();
            String[] streamParams = getStreamParams(stream);
            String groupId = streamParams[1];
            String userId = streamParams[2];
            CMDSosRequest cmdSosRequest = new CMDSosRequest();
            cmdSosRequest.setT(groupId);
            cmdSosRequest.setF(userId);
            ChatLogicManager.processMT59_VIDEO_SUCCESS_NOTIFICATION$REQ_B$TO$Message(cmdSosRequest);
        }
        LoggerFactory.getLog().info("[RtcStreamNotify]   -> " + prRequest);
        String str = JSONObject.toJSON(RtcCommonAck.authSuccess()).toString();
        resp.getWriter().write(str);
        resp.setStatus(200);
    }
    private String[] getStreamParams(String params) {
        String[] strings = params.split("&");
        StringBuilder sb = new StringBuilder();
        for (String string : strings) {
            if (string.contains("stream")) {
                sb.append(string);
                break;
            }
        }
        String strData = sb.toString();
        if (!CommonUtils.isStringEmpty(strData)) {
            String[] strArray = strData.split("=");
            if (strArray.length >= 2) {
                String s = strArray[1];
                return s.split("-");
            } else {
                return new String[0];
            }
        } else {
            return new String[0];
        }
    }

}
